<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
        integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    <!-- 可选的 Bootstrap 主题文件（一般不用引入） -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap-theme.min.css"
        integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"
        integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
        crossorigin="anonymous"></script>
    <title>Document</title>
    <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
    <script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
    <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
    <script src="JKZL.js"></script>
    <script src="axios.mm.js"></script>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0
        }
        .carousel-inner a{display:block;width:100%;height: 450px;margin: 0 auto;background-position:center; }
    </style>

<body> 
    <div>
            <span id="u-ipt"></span>:你好。<a href="javascript:;" id="logout">退出</a>
        </div>
        <div>
            <a href="login.html">请登录</a>
        </div>
        <div id="send">
            <input type="text" value="" />
            <input type="text" value="" />
            <input type="text" value="" />
            <input type="text" value="" />
            <input type="button" value="发布" id="ok" />
        </div>
        <ul class="list">
                
            <!-- <li>
                <input type="checkbox" name="" id="" class="ck">
            <h2></h2>
            <p></p>
            <p></p>
            <img src="" alt="">
            <a data-id="" href="javascript:;" class="d-btn">删除</a>
        </li> -->
    
        </ul>
    <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
        <!-- Indicators -->
        <ol class="carousel-indicators">
            <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
            <li data-target="#carousel-example-generic" data-slide-to="1"></li>
            <li data-target="#carousel-example-generic" data-slide-to="2"></li>
            <li data-target="#carousel-example-generic" data-slide-to="3"></li>
           
        </ol>

        <!-- Wrapper for slides -->
        <div class="carousel-inner" role="listbox">
            <div class="item active">
                <a href="iten.html"
                    style="background-image:url('https://mall02.sogoucdn.com/image/2019/05/21/20190521183316_6120.png');"></a>
                <div class="carousel-caption">

                </div>
            </div>
            <div class="item">
                <a href="iten.html"
                    style="background-image:url('https://mall03.sogoucdn.com/image/2019/05/13/20190513192844_4981.jpg')"></a>
                <div class="carousel-caption">

                </div>
            </div>
            <div class="item">
                <a href="iten.html"
                    style="background-image:url('https://mall02.sogoucdn.com/image/2019/05/13/20190513191021_4976.png');"></a>
                <div class="carousel-caption">

                </div>
            </div>
            <div class="item">
                <a href="iten.html"
                    style="background-image:url('https://mall03.sogoucdn.com/image/2019/03/18/20190318094353_4589.png');"></a>
                <div class="carousel-caption">

                </div>
            </div>
           

        </div>

        <!-- Controls -->
        <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
            <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
            <span class="sr-only">Previous</span>
        </a>
        <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
            <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
        </a>
    </div>

   
        <script>
            let token = localStorage.getItem('token')
            let username = localStorage.getItem('username')
            let oDivs = document.querySelectorAll('div')
            //如果已经登录 第一个div 显示
            if (token) {
    
                oDivs[0].style.display = 'block'
                oDivs[1].style.display = 'none'
            } else {
                oDivs[0].style.display = 'none'
                oDivs[1].style.display = 'block'
            }
    
            //点击删除按钮移除用户名id 然后跳转登录界面
            let logOut = document.querySelector('#logout')
            logOut.onclick = function () {
                localStorage.removeItem('token')
                localStorage.removeItem('username')
                location.href = 'login.html'
                
            }
    
    
            // 首页列表
            let pagenum = 0
            let obj = {}
            if (token) {
                obj.uid = token
            }
    
            loadData()
    
            //点击事件：发布商品 显示商品列表
    
    
            let ok = document.querySelector('#ok')
            ok.onclick = function () {
    
                let ipts = document.querySelectorAll('#send input')
                let params = {
                    pimg: ipts[0].value,
                    pname: ipts[1].value,
                    pprice: ipts[2].value,
                    pdesc: ipts[3].value
                }
                if (token) {
                    params.uid = token
    
                }
                axios.get(goodsAddApi, { params }).then(res => {
                    console.log(res)
                    loadData()
                })
    
            }
            //封装一个函数，连接可以看见首页商品的接口，
            function loadData() {
                axios.get(proListApi, {
                    params: obj
                }).then(res => {
                    console.log(res)
                    let html = ''
                    res.data.data.forEach(p => {
                        html += `
                    <li>
                            <a href="iten.html?pid=${p.pid}">                    
                            <h2>${p.pname}</h2>
                        </a>
                        <p>${p.pprice}</p>
                        <p>${p.pdesc}</p>
                        <img src="${p.pimg}" alt="">
                        <a data-id="${p.pid}" href="javascript:;" class="d-btn">删除</a>
                    </li>
                
                `
    
                    });
                    let list = document.querySelector('.list')
                    list.innerHTML = html;
                })
    
            }
            //连接删除商品的接口
            let list = document.querySelector('.list')
            list.onclick = function (evt) {
                if (evt.target.classList.contains('d-btn')) {
                    let pid = evt.target.getAttribute('data-id')
                    console.log(pid)
                    axios.get(goodsDelApi, {
                        params: { pid }
                    }).then(res => {
                        console.log(res.data)
                        loadData()
                    })
    
                }
    
            }
    
           
        </script>
</body>

</html>